package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.ManifestoDto;
import com.njworkorder.Entity.AdministrativeOrganization;
import com.njworkorder.Entity.AdministrativePersonnel;
import com.njworkorder.Entity.Annualpln;
import com.njworkorder.Entity.Manifesto;
import com.njworkorder.Mapper.ManifestoMapper;
import com.njworkorder.Service.ManifestoService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.MainifesttoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Tag(name = "清单配置接口", description = "清单配置相关接口")
@RestController
@RequestMapping("/man")
public class ManifestoController {

    private final ManifestoMapper manifestoMapper;
    private final ManifestoService manifestoService;
    public ManifestoController(ManifestoService manifestoService,
                               ManifestoMapper manifestoMapper) {
        this.manifestoService = manifestoService;
        this.manifestoMapper = manifestoMapper;
    }

    @Operation(summary = "获取清单配置列表")
    @GetMapping("/getManList")
    public Result<Page<Manifesto>> getCIList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize) {
        LambdaQueryWrapper<Manifesto> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.apply("1=1");


        Page<Manifesto> page = new Page<>(pageIndex, pageSize);
        Page<Manifesto> page1 = manifestoService.page(page, queryWrapper);

        return ResultUtil.success(page1);
    }
    @Operation(summary = "新增清单配置列表")
    @PostMapping("/ManInfo")
    public Result<String> addInfo(@RequestBody ManifestoDto manifestoDto,
                                  @RequestHeader("Create-Ap-Id") String createApId){

        LambdaQueryWrapper<Manifesto> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Manifesto::getSectionId,manifestoDto.getSectionId());
        long count = manifestoService.count(lambdaQueryWrapper);
        if (count>0){
            return ResultUtil.fail("路段已存在!");
        }

        Manifesto Ma = new Manifesto();
        BeanUtils.copyProperties(manifestoDto,Ma);
        String id = UUID.randomUUID().toString();
        Ma.setId(id);
        Ma.setCreateApId(createApId);

        boolean save = manifestoService.save(Ma);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.fail("添加失败");
    }
    @Operation(summary = "根据id删除清单配置")
    @GetMapping("/deleteManId")
    public Result<String> deleteManId(@RequestParam("id") String id){
        boolean removeById = manifestoService.removeById(id);
        if (removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }
    @Operation(summary = "根据id修改清单配置")
    @PostMapping("/updateManId/{id}")
    public Result<String> updateManId(@RequestBody ManifestoDto manifestoDto,
                                     @PathVariable("id") String id){
        Manifesto MA = new Manifesto();
        MA.setId(id);

        BeanUtils.copyProperties(manifestoDto, MA);

        boolean b = manifestoService.updateById(MA);
        if (b){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }


    @Operation(summary = "根据id查询清单配置信息")
    @GetMapping("/queryInfo")
    public Result<Manifesto> queryInfo(@RequestParam("id") String id){
        Manifesto list = manifestoService.getById(id);
        return ResultUtil.success(list);
    }





        @Operation(summary = "清单配置树形结构")
        @GetMapping("/getContents")
        public Result<List<MainifesttoVO>> getContents() {

            LambdaQueryWrapper<Manifesto> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(Manifesto::getId,Manifesto::getTheyears, Manifesto::getParentId, Manifesto::getInstitution,Manifesto::getInformation,Manifesto::getContractStartTime,Manifesto::getContractEndTime,Manifesto::getSectionId);
            queryWrapper.apply("1=1");
            List<Manifesto> list = manifestoService.list(queryWrapper);

            List<Manifesto> listF = list.stream().filter(item -> item.getParentId().isEmpty() || item.getParentId().isBlank()).toList();
            List<MainifesttoVO> listLevelVo = new ArrayList<>();
            MainifesttoVO mainifesttoVO;
            for (Manifesto item : listF) {
                mainifesttoVO = new MainifesttoVO();
                BeanUtils.copyProperties(item, mainifesttoVO);
                mainifesttoVO.setGrade(1);
                mainifesttoVO.setChildren(getAllAoLevel(item.getId(), list, 2));
                listLevelVo.add(mainifesttoVO);
            }
            return ResultUtil.success(listLevelVo);
        }

        public List<MainifesttoVO> getAllAoLevel(String id, List<Manifesto> list, int grade) {
            List<Manifesto> list1 = list.stream().filter(item -> item.getParentId().equals(id)).toList();
            List<MainifesttoVO> listLevelVo = new ArrayList<>();
            MainifesttoVO mainifesttoVO;
            for (Manifesto item : list1) {
                mainifesttoVO = new MainifesttoVO();
                BeanUtils.copyProperties(item, mainifesttoVO);
                mainifesttoVO.setGrade(grade);
                mainifesttoVO.setChildren(getAllAoLevel(item.getId(), list, grade + 1));
                listLevelVo.add(mainifesttoVO);
            }
            return listLevelVo;
        }

    }
